package com.qingxin.sys.support;

import java.util.Vector;

/**
 * 线程管理器，提供对自定义线程的统一管理，参见：{@link com.qingxin.sys.support.BaseThread}
 * @author Administrator
 *
 */
public class BaseThreadManager {
	
	private static BaseThreadManager manager;
	
	public static BaseThreadManager getInstance(){
		if(manager == null){
			manager = new BaseThreadManager();
		}
		return manager;
	}
	
	
	private Vector<BaseThread> threads = new Vector<BaseThread>();
	
	private BaseThreadManager() {
	}
	
	
	/**
	 * 添加线程
	 * @param baseThread
	 */
	public void add(BaseThread baseThread){
		threads.add(baseThread);
		if(!baseThread.isRunning()){
			try {
				baseThread.start();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	
	/**
	 * 停止所有线程
	 */
	public void stopAll(){
		try {
			for(BaseThread baseThread : threads){
				try {
					baseThread.dispose();
				} catch (Exception e) {
				}
			}
			threads.clear();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
